<template>
  <el-button @click="handleAdd" v-if="$filters.auth($filters.PrivilegeType.Add)" class="button-header"  type="primary" :icon="Plus">添加品牌</el-button>
  <el-form inline :model="query" class="filter" label-width="100px">
    <el-form-item label="品牌名称：">
      <el-col :span="22">
        <el-input v-model="query.name" placeholder="请输入品牌名称" />
      </el-col>
    </el-form-item>
    <el-form-item class="filter-actions">
      <el-button class="button-small" type="primary" @click="handleSearch">筛选</el-button>
      <el-button type="text" class="button-reset" @click="handleClear">重置筛选条件</el-button>
    </el-form-item>
  </el-form>
  <grid url="meeting/brand/list" :params="query" orderbyName='Sequence' ref="datalist">
    <el-table-column prop="name" label="品牌名称" />
    <el-table-column label="排序" sortable prop="sequence">
      <template #default="scope">
        <el-input-number v-model="scope.row.sequence" controls-position="right" @change="handleChangeSequence(scope.row)" :min="0" :max="99999999" />
      </template>
    </el-table-column>
    <el-table-column label="是否合作伙伴" sortable prop="sequence" width="80">
        <template #default="scope">
          <el-switch v-model="scope.row.isPartner" @change="changePartner(scope.row)" />
        </template>
      </el-table-column>
    <el-table-column label="操作" align="right" width="200"  v-if="$filters.auth($filters.PrivilegeType.Edit) || $filters.auth($filters.PrivilegeType.Delete)">
      <template #default="scope">
        <el-button type="text" v-if="$filters.auth($filters.PrivilegeType.Edit)" @click="handleEdit(scope.row)">编辑</el-button>
        <el-popconfirm :width="300" v-if="$filters.auth($filters.PrivilegeType.Delete)" class="btn-popconfirm" @confirm="handleDelete(scope.row.id)" confirmButtonText="确认" cancelButtonText="取消" title="确定要删除该员工吗？">
          <template #reference>
            <el-button type="text" size="small">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </grid>
  <el-dialog append-to-body v-model="brandForm.dialogVisible" :title="brandForm.form.id > 0 ? '修改品牌':'添加品牌'" width="30%">
    <el-form ref="postForm" label-width="150px" class="ruleForm" :model="brandForm.form" :rules="brandForm.rules">
      <el-form-item label="品牌名称：" prop="name">
          <el-col :span="22">
            <el-input v-model="brandForm.form.name" maxlength="20" show-word-limit placeholder="最多可输入20个字符" />
          </el-col>
        </el-form-item>
        <el-form-item label="品牌Logo：" prop="logo">
          <choose-file :key="1" tips="建议尺寸200x400像素" :limit="1" v-model="brandForm.form.logo" />
        </el-form-item>
        <el-form-item label="序号：">
          <el-col :span="22">
            <el-input-number v-model="brandForm.form.sequence" :min="0" controls-position="right"/>
          </el-col>
        </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="brandForm.dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave(postForm)">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import {ref} from 'vue'
import Grid from '@/components/Grid.vue'
import {Plus} from '@element-plus/icons-vue'
import {ElForm, ElMessage} from 'element-plus'
import {BrandSave, UpdatetBrandSequence,BrandDelete,UpdatetBrandPartner} from '@/api/site'
import ChooseFile from '@/components/ChooseFile.vue'
const query = ref<BrandQuery>({
  name: '',
  firstLetter:''
})
const handleAdd = ():void => {
  brandForm.value.form =  {
    id: 0,
    name: '',
    sequence: 0,
    firstLetter:'',
    isPartner:false,
    logo:''
  }
  brandForm.value.dialogVisible = true
  brandForm.value.title ='添加品牌'
}
const handleEdit = (row: Brand):void => {
  brandForm.value.form =  {
    id: row.id,
    name: row.name,
    sequence: row.sequence,
    firstLetter:  row.firstLetter,
    isPartner:  row.isPartner,
    logo: row.logo
  }
  brandForm.value.form.logo = row.logo
  brandForm.value.dialogVisible = true
  brandForm.value.title ='编辑品牌'
}
type FormInstance = InstanceType<typeof ElForm>
const postForm = ref<FormInstance>()
const datalist = ref<InstanceType<typeof Grid>>()
const handleSearch = ():void => {
  datalist.value?.loadData()
}
const handleClear = ():void => {
  query.value.name = ''
  datalist.value?.loadData()

}
const brandForm = ref<DialogForm<Brand>>({
  title:'添加品牌',
  dialogVisible: false,
  form: {
    id: 0,
    name: '',
    sequence: 0,
    firstLetter:'',
    isPartner:false,
    logo:''
  },
  rules: {
    name: [{ required: true, message: '请输入品牌名称', trigger: 'blur' }],
    logo: [{ required: true, message: '请上传品牌Logo', trigger: 'change' }],
  }
})
/**
 * 修改排序
 * @param row 
 */
const handleChangeSequence = (row:Brand):void => {
  UpdatetBrandSequence(row.id,row.sequence).then((res:ServiceResponse<any>) => {
    if(res.success) {
          ElMessage.success('保存成功')
          datalist.value?.loadData()
        } else  ElMessage.error(res.msg)
  })
}
/**
 * 保存品牌
 * @param formEl 
 */
const handleSave = (formEl: FormInstance | undefined): void => {
  if (!formEl) return
  formEl.validate((valid) => {
    if(valid) {
      BrandSave(brandForm.value.form).then((res:ServiceResponse<any>) => {
        if(res.success) {
          ElMessage.success('保存成功')
          datalist.value?.loadData()
          brandForm.value.dialogVisible = false
        } else  ElMessage.error(res.msg)
      })
    }
  })
}
/**
 * 删除品牌
 * @param id 
 */
const handleDelete = (id:number):void => {
  BrandDelete(id).then(((res:ServiceResponse<any>) => {
    if(res.success) {
          ElMessage.success('删除成功')
          datalist.value?.loadData()
        } else  ElMessage.error(res.msg)
  }))
}
/**
 * 是否合作伙伴
 * @param row 
 */
const changePartner = (row:Brand) => {
  UpdatetBrandPartner(row.id,row.isPartner).then((res:ServiceResponse<any>) => {
    if(res.success) {
          ElMessage.success('保存成功')
          datalist.value?.loadData()
        } else  ElMessage.error(res.msg)
  })
}
</script>
<style lang="scss" scoped>
@import "@/assets/styles/mixin.scss";
@import "@/assets/styles/tablePage.scss";
</style>